#include "CString.h"

CString::CString()
{

}

CString::CString(const QString &str)
{
    this->append(str);
}

CString::CString(const uint8_t *num, uint32_t len)
{
    for (uint32_t i = 0; i < len; ++i)
    {
        this->append(QString::number(num[i], 16).rightJustified(2, '0'));
    }
}

CString::CString(const QTextEdit &TextEdit)
{
    this->append(TextEdit.toPlainText());
}
uint8_t char2uint8(const char c)
{
    if((c >= '0') && (c <= '9'))
    {
        return c - '0';
    }else if((c >= 'a') && (c <= 'f')){
        return c - 'a';
    }else if((c >= 'A') && (c <= 'F')){
        return c - 'A';
    }else{
        return 0;
    }
}

CString::CString(const char *unicode, int size)
{
    uint8_t buf[256] = {0};
    if(size == -1)
    {
        size = strlen(unicode);
    }

    if(size == 0)
    {
        this->clear();
        return;
    }

    if(size % 2)
    {
        for(int i = 1;i < size + 1;++i)
        {
            if(i % 2)
            {
                buf[i / 2] += char2uint8(unicode[i]);
            }else{
                buf[i / 2] = char2uint8(unicode[i]) << 4;
            }
        }
        size++;
    }else{
        for(int i = 0;i < size;++i)
        {
            if(i % 2)
            {
                buf[i / 2] += char2uint8(unicode[i]);
            }else{
                buf[i / 2] = char2uint8(unicode[i]) << 4;
            }
        }
    }

    for (int i = 0; i < (size / 2); ++i)
    {
        this->append(QString::number(buf[i], 16).rightJustified(2, '0'));
    }
}

void CString::addspace()
{
    uint max = this->size() / 2;
    if ((this->size() % 2) == 0)
    {
        max--;
    }
    for (uint i = max; i > 0 ; --i)
    {
        this->insert(2 * i, " ");
    }
}

void CString::subspace()
{
    for (uint32_t i = this->size(); i > 0; --i)
    {
        this->remove(' ');
    }
}

void CString::touint8(uint8_t *num)
{
    bool ok;
    CString str = *this;
    if(str.size() % 2)
    {
        str.prepend('0');
    }
    for (int i = 0; i < str.size() / 2; ++i)
    {
        num[i] = (uint8_t)(str.mid(i * 2, 2).toInt(&ok, 16));
    }
}

void CString::print_TextEdit(QTextEdit &TextEdit)
{
    CString str = this->toUpper();
    str.addspace();
    TextEdit.setText(str);
}

void CString::scanf_TextEdit(QTextEdit &TextEdit)
{
    this->clear();
    this->append(TextEdit.toPlainText());
}



CString &CString::operator = (const QString &str)
{
    this->clear();
    this->append(str);
    return *this;
}

CString &CString::operator =(const char *unicode)
{
    uint8_t buf[256] = {0};

    int size = strlen(unicode);

    if(size == 0)
    {
        this->clear();
        return *this;
    }

    if(size % 2)
    {
        for(int i = 1;i < size + 1;++i)
        {
            if(i % 2)
            {
                buf[i / 2] += char2uint8(unicode[i]);
            }else{
                buf[i / 2] = char2uint8(unicode[i]) << 4;
            }
        }
        size++;
    }else{
        for(int i = 0;i < size;++i)
        {
            if(i % 2)
            {
                buf[i / 2] += char2uint8(unicode[i]);
            }else{
                buf[i / 2] = char2uint8(unicode[i]) << 4;
            }
        }
    }

    for (int i = 0; i < (size / 2); ++i)
    {
        this->append(QString::number(buf[i], 16).rightJustified(2, '0'));
    }
    return *this;
}


